queueMicrotask() is a standard API that directly schedules a microtask, while Promise.resolve().then() is a 'hack' that uses a resolved Promise to achieve the same effect, but with more overhead and different error-handling semantics.
In JavaScript, queueMicrotask() is a standard API (ES2020+) that allows you to schedule a function to run as a microtask. Microtasks are special callbacks that execute after the currently executing script completes but before the browser handles any pending tasks, such as rendering or I/O . Both queueMicrotask() and Promise.resolve().then() can be used to schedule microtasks, but they differ in their API design, performance characteristics, and error-handling behavior.
Semantic Clarity: queueMicrotask() clearly expresses the intent to schedule a microtask, while Promise.resolve().then() is a workaround that obscures the actual purpose .
Performance: queueMicrotask() is more lightweight because it doesn't need to create a resolved Promise instance, reducing memory allocation and overhead .
Error Handling: If the callback in queueMicrotask() throws an error, it becomes an unhandled rejection (or triggers the error event). In contrast, a .then() callback's error is caught and transformed into a rejected Promise, which may lead to silent failures if not explicitly handled .
API Simplicity: queueMicrotask() takes a single function argument and returns nothing, while Promise.resolve().then() returns a new Promise, enabling chaining even when not needed .
Standardization: queueMicrotask() is the official standard way to schedule microtasks, recognized by WHATWG and ECMAScript, whereas the Promise-based approach was always a community convention .
Before queueMicrotask() was standardized, developers commonly used Promise.resolve().then() to schedule microtasks. However, this approach had downsides: it created unnecessary Promise objects, had semantic ambiguity, and its error behavior could be confusing . The introduction of queueMicrotask() provides a dedicated, clear, and more efficient API for this specific need . It's now supported in all modern browsers and Node.js 11+ .
You should prefer queueMicrotask() when you need to schedule a microtask for performance-critical code, in libraries, or when you want explicit semantics . Use Promise.resolve().then() when you're already working within a Promise chain or when you need the chaining capability that Promises provide . For simple microtask scheduling with no need for chaining, queueMicrotask() is the better choice.